package com.library_management.dao.impl;

import com.library_management.dao.UserDao;
import com.library_management.model.User;  // 实体类包路径修正
import com.library_management.jdbc.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public User selectByUsername(String username) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return null;
            }

            // 修正SQL语法（补全WHERE条件）
            String sql = "SELECT id, username, password, name, role, status " +
                    "FROM user WHERE username = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);  // 绑定参数

            rs = pstmt.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setRole(rs.getString("role"));
                user.setStatus(rs.getInt("status"));  // 修正字段名拼写（原statrs→status）
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt,conn);  // 正确关闭资源
        }

        return user;
    }

    @Override
    public int insert(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int rowsAffected = 0;

        try {
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return rowsAffected;
            }

            // 修正SQL语法（补全VALUES）
            String sql = "INSERT INTO user (username, password, name, role, status) " +
                    "VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getName());
            pstmt.setString(4, user.getRole());
            pstmt.setInt(5, user.getStatus());  // 修正参数索引

            rowsAffected = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源（ResultSet为null时直接传null）
            JdbcUtil.close(null, pstmt,conn);
        }

        return rowsAffected;
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int rowsAffected = 0;

        try {
            conn = JdbcUtil.getConnection();
            if (conn == null) {
                System.err.println("获取数据库连接失败：conn为null");
                return rowsAffected;
            }

            String sql = "UPDATE user SET status = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, status);
            pstmt.setInt(2, id);  // 绑定参数

            rowsAffected = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, pstmt,conn);
        }

        return rowsAffected;
    }
}